﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Security;

namespace NearForums.Web.Extensions.FormsAuthenticationHelper.Impl
{
    public class AccountMembershipService : IMembershipService
    {
        private MembershipProvider _provider;

        public AccountMembershipService()
            : this(null)
        {
        }

        public AccountMembershipService(MembershipProvider provider)
        {
            _provider = provider ?? Membership.Provider;
        }

        public int MinPasswordLength
        {
            get
            {
                return _provider.MinRequiredPasswordLength;
            }
        }

        public bool ValidateUser(string userName, string password)
        {
            return _provider.ValidateUser(userName, password);
        }

        public MembershipCreateStatus CreateUser(string userName, string password, string email)
        {
            MembershipCreateStatus status;
            _provider.CreateUser(userName, password, email, null, null, true, null, out status);
            return status;
        }

		public bool ChangePassword(string userName, string oldPassword, string newPassword)
		{
			MembershipUser currentUser = _provider.GetUser(userName, true /* userIsOnline */);
			return currentUser.ChangePassword(oldPassword, newPassword);
		}

		/// <summary>
		/// Changes the password of a user without asking the old password.
		/// </summary>
		public bool ChangePassword(string userName, string newPassword)
		{
			var currentUser = _provider.GetUser(userName, true);
			var oldPassword = currentUser.ResetPassword();
			return currentUser.ChangePassword(oldPassword, newPassword);
		}

		public string ResetPassword(string userName, string secretAnswer)
		{
			return _provider.ResetPassword(userName, secretAnswer);
		}

	}
}
